package com.zzy.command;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
	private static final String FILE= "student.txt";
	public static ArrayList<student> studentlist=new ArrayList<student>();
	public static Command command=new nothing();
	public static void main(String args[])
	{
		
		system control=new system();
		load();
		 Scanner scanner = new Scanner(System.in);
	     int num;

	        do {
	            System.out.println("学生信息管理程序");
	            System.out.println("1、添加一个新的学生信息");
	            System.out.println("2、修改一个已有的学生信息");
	            System.out.println("3、删除一个已有的学生信息");
	            System.out.println("4、撤销上一次操作");
	            System.out.println("5、列出全部学生信息");
	            System.out.println("6、退出程序");
	            System.out.print("你选择的操作是：");

	            num = scanner.nextInt();

	            switch (num) {
	                case 1:
	                    add acc=new add(studentlist);
	                    control.setCommand(acc);
	                    control.Task();
	                    break;
	                case 2:
	                    modify mcc=new modify(studentlist);
	                    control.setCommand(mcc);
	                    control.Task();
	                    break;
	                case 3:
	                    delete dcc=new delete(studentlist);
	                    control.setCommand(dcc);
	                    control.Task();
	                    break;
	                case 4:
	                    control.undo();
	                    break;
	                case 5:
	                    list();
	                    break;
	                case 6:
	                    save();
	                    System.out.println("程序结束");
	                    break;
	                default:
	                    System.out.println("无效操作");
	                    break;
	            }

	        } while (num != 6);

	        scanner.close();
	}
	public static void list()
	{
		if(studentlist.isEmpty()) System.out.println("目前学生信息为空");
		for(student s:studentlist)
		{
			System.out.printf("Name:"+s.getName()+" Age:");
			System.out.print(s.getAge());
			System.out.print(" ID:");
			System.out.println(s.getPass());
		}
	}
	public static  void load() {
	    try (BufferedReader reader = new BufferedReader(new FileReader(FILE))) {
	        String line;
	        while ((line = reader.readLine()) != null) {
	            String[] parts = line.split(",");
	            if (parts.length == 3) {
	                studentlist.add(new student(parts[0], Integer.parseInt(parts[1].trim()), parts[2].trim()));
	            }
	        }
	    } catch (IOException e) {
	        System.out.println("加载学生信息失败");
	    }

	}
	public static void save() {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE))) {
            for (student student : studentlist) {
                writer.write(student.toString());
                writer.newLine();
            }
        } catch (IOException e) {
            System.out.println("学生信息保存失败");
        }
  }

}
